package cn.zhihong.controller;

import cn.zhihong.common.R;
import cn.zhihong.pojo.Category;
import cn.zhihong.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CategoryController
 * @Author 庄志宏
 * @Date 2022/4/22 12:54
 */
@RestController
@Slf4j
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /**
     * @author 庄志宏
     * @date: 2022/4/22 13:05
     * 分类管理，新添菜品或套餐
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        log.info("category为：{}",category);
        categoryService.save(category);
        return R.success("新增成功");
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/22 19:57
     * 菜品分类分页查询
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize){
        log.info("菜品分类查询page:{};pageSize{}",page,pageSize);
        Page<Category> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/22 21:32
     * 根据id删除分类
     */
    @DeleteMapping
    public R<String> delect(Long ids){
        categoryService.myRemoveById(ids);
        return R.success("删除成功");

    }
    /**
     * @author 庄志宏
     * @date: 2022/4/23 10:45
     * 修改分类
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改{}分类",category);
        categoryService.updateById(category);
        return R.success("分类修改成功");
    }

    @GetMapping("/list")
    public R<List<Category>> type(Category category){
        log.info("菜品分类的：{}",category);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(category.getType() != null ,Category::getType,category.getType());
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
